﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;


namespace WebApplication3
{
    public partial class WebForm4 : System.Web.UI.Page
    {
        static string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename='|datadirectory|\Database1.mdf';Integrated Security=True;User Instance=True";
        static SqlConnection cn = new SqlConnection(connectionString);
        string sCommand;
        static SqlDataAdapter da;
        static DataTable dataTable = new DataTable();
        string[,] dataUser = new string[1, 2];
        Boolean status = false;

        protected void Page_Load(object sender, EventArgs e)
        {
                    

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //System.Console.WriteLine("-----------------------------------------test bouton-------------------------------------------");
            System.Diagnostics.Debug.WriteLine("-------------------buton click--------------------------");
            try
            {
                //Command to execute           
                sCommand = "SELECT username,password FROM Client";
                //sCommand = "SELECT * FROM Client";
                System.Diagnostics.Debug.WriteLine("connexion vat etre etabli");
                da = new SqlDataAdapter(sCommand, cn);
                System.Diagnostics.Debug.WriteLine("connexion etabli");
                int recordsAffected = da.Fill(dataTable);
                System.Diagnostics.Debug.WriteLine(".fill effectuer");
                //Remplit le tableau de données
                if (recordsAffected > 0)
                {
                    System.Diagnostics.Debug.WriteLine("tableau rempli");
                    foreach (DataRow dr in dataTable.Rows)
                    {

                        string s = dr[1].ToString();
                        System.Diagnostics.Debug.Write("BDD username:"+dr[0].ToString());
                        System.Diagnostics.Debug.Write("BDD password:"+dr[1].ToString());
                        System.Diagnostics.Debug.Write("username:" + UserName.Text);
                        System.Diagnostics.Debug.Write("password:" + Password.Text);
                        if (UserName.Text.Equals(dr[0].ToString().Trim()))
                        {
                            //string s2 = dr[2].ToString();
                            if (Password.Text.Equals(dr[1].ToString().Trim()))
                            {
                                //MessageBox.Show("Connexion réussie ==> Bienvenue " + dr[1]);
                                status = true;
                                //System.Console.WriteLine("test");
                                System.Diagnostics.Debug.Write("------------connecter---------------");
                                Session["UserName"] = UserName.Text;
                            }

                        }

                        status = false;
                    }

                    if (status == false)
                    {
                        //MessageBox.Show("Login et/ou mot de passe incorrect(s)");

                    }

                }
            }
            catch (SqlException ex)
            {
                string msg = "";
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + "Message : " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
            }
            finally
            {

                cn.Close();
            }
        }
    }
}


            /*
            // Déclaration de l'objet de connexion
            System.Data.SqlClient.SqlConnection oConnection;

            // Création de l'objet de connexion
            oConnection = new System.Data.SqlClient.SqlConnection();

            // Ecriture de la chaîne de connexion
            string sConnectionString;
            sConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename='|datadirectory|\Database1.mdf';Integrated Security=True;User Instance=True"; 
            oConnection.ConnectionString = sConnectionString;
            // Ouverture de la connexion
            oConnection.Open();

            // Traitement des opération à effectuer sur la base de données
            System.Diagnostics.Debug.Write("------------connecter---------------");


            // Fermeture de la connexion
            oConnection.Close();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            System.Console.WriteLine("-----------------------------------------test bouton-------------------------------------------");
        }
      

    }
}*/